<template>
  <!--  -->
  <el-dialog
    title="编辑体征信息"
    :visible="value"
    width="700px"
    custom-class="dialog-box"
    @close="onClose"
    @open="onOpen"
    :close-on-click-modal="false"
    :append-to-body="true"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      @submit.native.prevent="onSubmit"
    >
      <el-form-item v-if="type==1" label="呼吸：" prop="breathing">
        <el-input class="sugar_input" v-model="form.breathing">
          <template slot="append">(次/分钟)</template>
        </el-input>
      </el-form-item>
      <el-form-item v-if="type==2" label="脉搏：" prop="pulse">
        <el-input class="sugar_input" v-model="form.pulse">
          <template slot="append">(次/分钟)</template>
        </el-input>
      </el-form-item>
      <el-button v-show="false" :loading="btnLoading" native-type="submit">提交</el-button>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Valid from '@/_js/validate'
export default {
  props: ["value", "type", "curItem"],
  data() {
    return {
      form: {
        pulse: "",
        breathing: "",
        patientId: "",
        signId: ""
      },
      rules: {
        breathing:[{validator: Valid.integerNum, trigger: "blur",message:"请输入正确的呼吸值" }],
        pulse:[{validator: Valid.integerNum, trigger: "blur",message:"请输入正确的脉搏值" }]
      },
      btnLoading: false
    };
  },
  methods: {
    onClose() {
      const th = this;
      th.$emit("input", false);
      th.$emit("success");
    },
    onOpen() {
      const th = this;
      th.$nextTick(function() {
        if (th.curItem) {
          th.form.breathing = th.curItem.breathing;
          th.form.pulse = th.curItem.pulse;
          th.form.patientId=th.curItem.patientId;
          th.form.signId=th.curItem.signId;
        }
      });
    },
    onSubmit() {
      const th = this;
      let j = {};
      th.btnLoading=true;
      Object.assign(j,th.form);
      th.$api.savePatientSign(j).then(res => {
        if (res.executed) {
          th.$message.success("保存成功");
          th.onClose();
          th.btnLoading=false;
        }else{
          th.$message.success(res.message);
          th.btnLoading=false;
        }
      }).catch(error=>{
        th.btnLoading=false;
      });
    }
  }
};
</script>